package email;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
/**
 *
 * @author Vicktor Filimonov <freest93@gmail.com>, 
 * @author Nikita Morgun <lordal1304@gmail.com>
 */

public class Sender {
    private static final String configFile = "email.sender.properties.ini";
    
    private static Properties loadProperties() throws FileNotFoundException, IOException{
        Properties result = new Properties();
        result.load(new FileInputStream(Sender.class.getResource(configFile).getPath()));
        
        return result;
    } 
    public static void sendMail(){
        Properties props;
        try{
            props = loadProperties();
        } catch(IOException e){
            Logger.getGlobal().warning("[ERROR] Load properties while sending mail:"+e.getMessage());
            return;
        } 
        try{
            Session session = Session.getDefaultInstance(props);
            Message message = new MimeMessage(session);
            message.setFrom();
            message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("lordal1304@gmail.com"));
            message.setSubject("Testing Email Server");
            message.setText("Sasai");
            Transport t = session.getTransport();
            t.connect();
            t.sendMessage(message, InternetAddress.parse("lordal1304@gmail.com"));
        }catch(MessagingException e){
            Logger.getGlobal().warning("[ERROR] Build message while sending mail:"+e.getMessage());
        }
        
        
    }
    public static void sendConfirmationMail(String to, String confrimRef){
        Properties props;
        try{
            props = loadProperties();
        } catch(IOException e){
            Logger.getGlobal().warning("[ERROR] Load properties while sending mail:"+e.getMessage());
            return;
        } 
        try{
            Session session = Session.getDefaultInstance(props);
            Message message = new MimeMessage(session);
            message.setFrom();
            message.setRecipients(Message.RecipientType.TO,
                                    InternetAddress.parse(to));
            message.setSubject("~>=Confirmation Request from Alpha Shop=<~");
            message.setText("Confirm your registration by link: "+confrimRef);
            Transport t = session.getTransport();
            t.connect();
            t.sendMessage(message, InternetAddress.parse(to));
        }catch(MessagingException e){
            Logger.getGlobal().warning("[ERROR] Build message while sending mail:"+e.getMessage());
            return;
        }
    }
        public static void sendReportMail(String to, String reportPath){
        Properties props;
        try{
            props = loadProperties();
        } catch(IOException e){
            Logger.getGlobal().warning("[ERROR] Load properties while sending mail:"+e.getMessage());
            return;
        } 
        PrintStream f;
        try{
            f = new PrintStream("D:\\log.txt");
            f.println(Sender.class.getResource(configFile).getPath());
            Session session = Session.getDefaultInstance(props);
            Message message = new MimeMessage(session);
            message.setFrom();
            f.println(message.getFrom()[0]);
            message.setRecipients(Message.RecipientType.TO,
                                    InternetAddress.parse(to));
            message.setSubject("Report from Alpha Shop");
            Multipart content = new MimeMultipart();
            BodyPart bodyPart = new MimeBodyPart();
            f.println(2);
            DataSource ds = new FileDataSource(reportPath);
            bodyPart.setDataHandler(new DataHandler(ds));
            bodyPart.setFileName(reportPath);
            content.addBodyPart(bodyPart);
            f.println(3);
            message.setContent(content);
            Transport t = session.getTransport();
            t.connect();
            t.sendMessage(message, InternetAddress.parse(to));
            f.println(to);
        }catch(Exception e){
            Logger.getGlobal().warning("[ERROR] Build message while sending mail:"+e.getMessage());
            return;
        }
    }
}
